package com.gamelife.web.domain;


import com.gamelife.web.constant.ResponseEnum;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Objects;

public class AjaxResult extends HashMap<String, Object>
{
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";

    /**
     * 初始化一个新创建的 AjaxResult 对象，使其表示一个空消息。
     */
    public AjaxResult()
    {
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg 返回内容
     */
    public AjaxResult(HttpStatus code, String msg)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg 返回内容
     * @param data 数据对象
     */
    public AjaxResult(Integer code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (!Objects.isNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

    /**
     * 通过ResponseEnum 返回统一对象信息
     * @param responseEnum
     * @param data
     */
    public AjaxResult(ResponseEnum responseEnum,Object data){
        super.put(CODE_TAG,responseEnum.getCode());
        super.put(MSG_TAG,responseEnum.getMsg());
        if (!Objects.isNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }


    public static AjaxResult success()
    {
        return new AjaxResult(ResponseEnum.OK,null);
    }
    public static AjaxResult success(Object data)
    {
        return new AjaxResult(ResponseEnum.OK,data);
    }
    public static AjaxResult success(String msg)
    {
        return new AjaxResult(ResponseEnum.OK,msg);
    }
    public static AjaxResult success(String msg, Object data)
    {
        return new AjaxResult(ResponseEnum.OK.getCode(),msg,data);
    }


    public static AjaxResult error()
    {
        return new AjaxResult(ResponseEnum.ERROR,null);
    }

    public static AjaxResult error(String msg)
    {
        return AjaxResult.error(msg, null);
    }

    public static AjaxResult error(String msg, Object data)
    {
        return new AjaxResult(ResponseEnum.ERROR.getCode(), msg, data);
    }

    public static AjaxResult error(Integer code,String msg, Object data)
    {
        return new AjaxResult(code, msg, data);
    }

    public static AjaxResult error(ResponseEnum responseEnum)
    {
        return new AjaxResult(responseEnum, null);
    }

    @Override
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }
}
